﻿using AutoMapper;
using MediatR;
using ProduceBasicData.Api.Application.Command.Workstation;
using ProduceBasicData.Domain.FactoryModeling;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.WorkStation
{
    /// <summary>
    /// 工位信息批量删除处理类
    /// </summary>
    public class DeleteRangeWorkstationHandler : IRequestHandler<DeletetRangeWorkstationCommand, ApiResult<int>>
    {
        private readonly IBaseRespository<WorkstationModel> stationRepo;
        private readonly IMapper mapper;

        public DeleteRangeWorkstationHandler(IBaseRespository<WorkstationModel> stationRepo,
            IMapper mapper)
        {
            this.stationRepo = stationRepo;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(DeletetRangeWorkstationCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            var stationIds = request.Ids.Split(',').Select(int.Parse).ToList();
            var stationlist = stationRepo.GetAll().Where(x => stationIds.Contains(x.Id)).ToList();

            foreach (var item in stationlist)
            {
                item.Isdel = true;
            }
            var r = await stationRepo.UpdateRange(stationlist);
            res.Data = r;
            res.Code = ApiEnum.Success;
            res.Msg = "删除成功";
            return res;
        }
    }
}
